import React, { Component } from 'react';
import { connect } from 'react-redux';
import Util from "../../utils/Util";
import * as DataName from "../../action/DataNameConfig";
import { setCommonSystemLoading } from "../../action/Action";
import { GetNotifyList, DeleteNotify } from "../../action/ActionSystem";
import { Radio, Card, Pagination, List } from 'antd';
const RadioGroup = Radio.Group;
class Main extends Component {
    constructor(props) {
        super(props);
        this.state = {
            value: -1
        }
        this.initData=(flag,pageNo,pageSize)=>{
            let { dispatch } = this.props;
            if(!flag){
                dispatch(setCommonSystemLoading(DataName.BASE_CARD_LOADING,true));
            }
            dispatch(setCommonSystemLoading(DataName.BASE_TABLE_LOADING,true));
            dispatch(GetNotifyList({pageNo,pageSize, isCheck: this.state.value===-1?null:this.state.value}));
        }
        this.onChange=(e)=>{
            this.setState({
                value: e.target.value
            },()=>{
                this.initData(1,0,10);
            })
        }
        this.deleteNotify=(item)=>{
            let { dispatch, BaseData } = this.props;
            let notifyList = BaseData[DataName.NOTIFY_LIST] || {};
            dispatch(setCommonSystemLoading(DataName.BASE_TABLE_LOADING,true));
            dispatch(DeleteNotify({id:item.id},notifyList.content || []));
        }
    }
    componentWillMount(){
        Util.setMenuOpenKey(this.props,"notify");
        this.initData(0,0,10);
    }
    render() {
        let { BaseData, CommonSystem } = this.props;
        let notifyList = BaseData[DataName.NOTIFY_LIST] || {};
        const pagination = Util.paginationInit(notifyList);
        return (
            <div className={"page-common page-notify"}>
                <Card title={"系统通知"} bordered={false} loading={CommonSystem[DataName.BASE_CARD_LOADING] || false}>
                    <div className={""} style={{paddingBottom: '20px'}}>
                        <h3>我的消息</h3>
                        <RadioGroup onChange={this.onChange} value={this.state.value}>
                            <Radio value={-1}>全部</Radio>
                            <Radio value={0}>未读</Radio>
                            <Radio value={1}>已读</Radio>
                        </RadioGroup>
                    </div>
                    <List
                        itemLayout="horizontal"
                        dataSource={notifyList.content || []}
                        loading={CommonSystem[DataName.BASE_TABLE_LOADING] || false}
                        renderItem={item => (
                            <List.Item actions={[<a onClick={()=>this.deleteNotify(item)}>删除</a>, <a>查看申报单</a>]}>
                                <List.Item.Meta
                                    avatar={item.isCheck?<span className={"tag-check"}>已读</span>:<span className={"tag-check read"}>未读</span>}
                                    title={<a href="https://ant.design">申报单异常：{item.wasteNo}</a>}
                                    description={<div>
                                        异常申报单（{item.wasteNo}）
                                        申报企业：{item.fromCompany}
                                        接收企业：{item.toCompany}
                                    </div>}
                                />
                                <div style={{width: '200px'}}>{Util.formateDateObj(item.createTime)}</div>
                            </List.Item>
                        )}
                        pagination={{
                            onChange: (current, pageSize)=>this.initData(1,current-1, pageSize),
                            onShowSizeChange: (current, pageSize)=>this.initData(1,current-1, pageSize),
                            ...pagination
                        }}
                    />
                </Card>
            </div>
        )
    }
}

export default connect(state=>({
    User: state.User,
    CommonSystem: state.CommonSystem,
    BaseData: state.BaseData
}))(Main);
